package com.mmd.manage.web;


import com.mmd.bean.CODE;
import com.mmd.bean.WebJsonBean;
import com.mmd.exception.GameException;
import com.mmd.manage.service.SupplierService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping(value = "/api/supplier")
@Controller
public class SupplierController {
	
	@Autowired
	private SupplierService supplierService;

	@RequestMapping(value = "getSupplierListPage")
	@ResponseBody
	public WebJsonBean getSupplierListPage(String keyWord, Integer pageNo, Integer pageSize){
		pageNo = pageNo == null ? 1 : pageNo;
		pageSize = pageSize == null ? 20 : pageSize;
		return new WebJsonBean(CODE.SUCCESS, supplierService.getSupplierListPage(keyWord, pageNo, pageSize));
	}
	
	@RequestMapping(value = "getSupplierDetail")
	@ResponseBody
	public WebJsonBean getSupplierDetail(String supplierCode){
		if (StringUtils.isEmpty(supplierCode)){
			throw new GameException(CODE.PARAMERROR);
		}
		return new WebJsonBean(CODE.SUCCESS, supplierService.getSupplierDetail(supplierCode));
	}
	
	@RequestMapping(value = "addSupplier")
	@ResponseBody
	public WebJsonBean addSupplier(String supplierName, String phone, String account, String pwd){
		if (StringUtils.isEmpty(supplierName)){
			throw new GameException(new CODE(9999, "请填写供应商名称"));
		}
		if (StringUtils.isEmpty(account)){
			throw new GameException(new CODE(9999, "请填写登录账号"));
		}
		if (StringUtils.isEmpty(pwd)){
			throw new GameException(new CODE(9999, "请填写供登录密码"));
		}
		supplierService.addSupplier(supplierName, phone, account, pwd);
		return new WebJsonBean(CODE.SUCCESS);
	}
	
	@RequestMapping(value = "getSupplierList")
	@ResponseBody
	public WebJsonBean getSupplierList(){
		return new WebJsonBean(CODE.SUCCESS, supplierService.getSupplierList());
	}
}
